package com.beamfield.controller.manage.mixstation;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.service.manage.mixstation.MixService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Date;

/**
 * @author Zephyr
 * @Description:拌和站首页
 * @Date 2018/9/20
 */
@Controller
@RequestMapping("/cms/mixstation")
public class MixController extends BaseController {

    @Autowired
    private MixService mixService;
    /**
     * 拌和站首页
     * @return
     */
    @RequestMapping(value = "/mix")
    public ModelAndView mix(){
        return new ModelAndView();
    }

    /**
     * 获取数据分析
     * @param request
     * @return
     */
    @RequestMapping(value = "/getDataAnalysis", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getDataAnalysis(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        try{
            return mixService.getDataAnalysis(beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取仓储管理
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMaterialInfo", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getMaterialInfo(HttpServletRequest request){
        String suppliesId = request.getParameter("supplies_id");
        Long beamId = super.getBeamId(request);
        try{
            return new ApiResult(mixService.getMaterialInfo(suppliesId, beamId));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取铁总统计信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMixTzStatistics", method = RequestMethod.GET)
    @ResponseBody
    public PageResult getMixTzStatistics(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        try{
            return mixService.getMixTzStatistics(beamId, offset, limit);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 导入铁总统计信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/importMixTzStatistics", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult importMixTzStatistics(HttpServletRequest request, @RequestParam("excelFile") MultipartFile multipartFile){
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        String statisticsTime = request.getParameter("statistics_time");
        if (StringUtils.isBlank(statisticsTime) || multipartFile == null) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try {
            Date statisticsDate = DateUtils.parseDate(statisticsTime, "yyyy-MM-dd HH:mm:ss");
            InputStream in = multipartFile.getInputStream();
            byte[] data = IOUtils.toByteArray(in);
            Workbook wb = null;
            try {
                wb = new HSSFWorkbook(new ByteArrayInputStream(data));
            } catch (Exception e) {
                wb = new XSSFWorkbook(new ByteArrayInputStream(data));
            }
            result = mixService.importMixTzStatistics(wb, userId, beamId, statisticsDate);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "导入成功");
        }
    }
}
